import java.util.*;
import java.io.*;

public class MP3Playlist implements Serializable{

    private static final long serialVersionUID = 1L;

    public String name;
    public int size;

    //public ArrayList<MP3File> playlist;

    public MP3File head;
    public MP3File tail;
    public MP3File current;
    private int currentPos;

    public HashMap<String, MP3File> hashMap;
    private int hashCapacity;

 
    public MP3Playlist(String n){
	name = n;
	size = 0;

	head = null;
	current = null;
	tail = null;
	//playlist = new Arraylist<MP3File>();

	hashCapacity = 64;
	float loadFactor = (float)0.64;
	hashMap = new HashMap<String, MP3File>(hashCapacity, loadFactor);
    }

    public void add(MP3File newTrack){
	if (head == null) {
	    head = newTrack;
	    tail = newTrack;
	    current = newTrack;
	    currentPos = 0;

	    newTrack.setNext(null);
	    newTrack.setPrev(null);
	}
	else {
	    if (head.toString().compareTo(newTrack.toString()) >=0) { //if newTrack belongs at the head
		head.setPrev(newTrack);
		newTrack.setNext(head);
		newTrack.setPrev(null);
		head = newTrack;
	    }
	    else if (tail.toString().compareTo(newTrack.toString()) <=0) { //if newTrack belongs at the tail
		tail.setNext(newTrack);
		newTrack.setNext(null);
		newTrack.setPrev(tail);
		tail = newTrack;
	    }
	    else if (current.toString().compareTo(newTrack.toString()) <0 ) {//if newTrack belongs after current
		while (current.getNext().toString().compareTo(newTrack.toString()) < 0) {
		    current = current.getNext();
		    currentPos++;
		}
		MP3File temp = current.getNext();
		current.setNext(newTrack);
		temp.setPrev(newTrack);
		newTrack.setNext(temp);
		newTrack.setPrev(current);
	    }
	    else {//if newTrack belongs before current
		while (current.getPrev().toString().compareTo(newTrack.toString()) > 0) {
		    current = current.getPrev();
		    currentPos--;
		}
		MP3File temp = current.getPrev();
		current.setPrev(newTrack);
		temp.setNext(newTrack);
		newTrack.setNext(current);
		newTrack.setPrev(temp);
	    }
	}

	//	playlist.add(newTrack);
	hashMap.put(newTrack.toString(), newTrack);
	size++;
    }
    
    public void remove(MP3File removeFile) {

    }

    public MP3File[] toArray() {
	MP3File[] temp = new MP3File[size];
	current = head;
	currentPos = 0;

	while(current != null){
	    temp[currentPos] = current;
	    current = current.getNext();
	    currentPos++;
	}
	return temp;
    }

}
